<?php
/**
 * Created by PhpStorm.
 * User: wpf
 * Date: 2018/3/5
 * Time: 15:47
 */

namespace Nuoche\Logic;

use Aliyun\Api\Dypls\Request\V20170525\BindAxnExtensionRequest;
use Aliyun\Api\Dypls\Request\V20170525\UnbindSubscriptionRequest;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Core\Exception\ClientException;
use Aliyun\Core\Config;
Config::load();

class AxnLogic
{
    static $acsClient = null;
    static $poolKey = 'FC100000027254001';

    public static function getAcsClient() {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dyplsapi";
        //产品域名,开发者无需替换
        $domain = "dyplsapi.aliyuncs.com";
        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = "LTAIJIy9YPACD858"; // AccessKeyId
        $accessKeySecret = "o7y402yprhDFJku1iwMUOaRnZUbGKN"; // AccessKeySecret
        // 暂时不支持多Region
        $region = "cn-hangzhou";
        // 服务结点
        $endPointName = "cn-hangzhou";
        if(static::$acsClient == null) {
            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }

    /**
     * 分机复用绑定
     *
     * @param $phoneNoA 对应绑定的A手机号码
     * @param $extension 对应分配的分机号(一般为3位数)
     * @param $expirationTime 绑定关系对应的失效时间-不能早于当前系统时间(格式：2018-02-29 18:00:00)
     * @param $outId 可选：业务自定义ID属性（选填：默认1）
     * @param $isRecording 可选:是否需要录制音频-默认是false
     * @return mixed
     */
    public function bindAxnExtension($phoneNoA,$extension,$expirationTime,$outId=1,$isRecording=false) {

        //组装请求对象
        $request = new BindAxnExtensionRequest();

        //必填:对应的号池Key
        $request->setPoolKey(static::$poolKey);

        //必填:AXN关系中的A号码
        $request->setPhoneNoA($phoneNoA);

        //可选:分机号，如果不填，自动分配1-3位
        $request->setExtension($extension);

        //必填:绑定关系对应的失效时间-不能早于当前系统时间
        $request->setExpiration($expirationTime);

        //可选:是否需要录制音频-默认是false
        $request->setIsRecordingEnabled($isRecording);

        //外部业务自定义ID属性
        $request->setOutId($outId);

        //hint 此处可能会抛出异常，注意catch
        $res = static::getAcsClient()->getAcsResponse($request);

        $Message = $res->Message;
        $requestId = $res->RequestId;
        $code = $res->Code;
        $secretBindDTO =  $res->SecretBindDTO;
        if($secretBindDTO){
            $extension = $secretBindDTO->Extension;
            $secretNo = $secretBindDTO -> SecretNo;
            $subsId  = $secretBindDTO -> SubsId;
        }
        \Think\Log::write('bindAxnExtension:: $phoneNoA:'.$phoneNoA.' $Message:'.$Message.' $requestId:'.$requestId.' $code:'.$code.' 绑定信息 subsId:'.$subsId.' sec:'.$secretNo.' ext'.$extension,'INFO');

        return $res;
    }

    function unbindSubscription($subsId, $secretNo) {
        //组装请求对象
        $request = new UnbindSubscriptionRequest();

        //必填:号池Key
        $request->setPoolKey(static::$poolKey);

        //必填:对应的产品类型
        $request->setProductType("AXN_171");

        //必填-分配的X小号-对应到绑定接口中返回的secretNo;
        $request->setSecretNo($secretNo);

        //必填-绑定关系对应的ID-对应到绑定接口中返回的subsId;
        $request->setSubsId($subsId);

        //hint 此处可能会抛出异常，注意catch
        $response = static::getAcsClient()->getAcsResponse($request);
        $Message = $response->Message;
        $requestId = $response->RequestId;
        $code = $response->Code;
        $chargeid = $response->ChargeId;

        \Think\Log::write('unbindSubscription::  $Message'.$Message.' $requestId:'.$requestId.' $code:'.$code.' ChargeId'.$chargeid,'INFO');

        return $response;
    }

}